package com.karst.webnotes.chapter04
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
	
	internal class TilingMaterialExample extends Sprite
	{
		public function TilingMaterialExample()
		{
			super();
			createView() ;
		}

		private var view : BasicView = null ;
		private var material : BitmapFileMaterial = null ;
		private var plane : Plane = null ;
		private function createView() : void
		{
			if( view == null )
			{
				view = new BasicView( 0, 0, true, true ) ;
				view.buttonMode = true ;
				view.addEventListener( Event.ENTER_FRAME, rateHandler ) ;
			}
			
			if( material == null )
			{
				material = new BitmapFileMaterial( "01.jpg" ) ;
				material.addEventListener( FileLoadEvent.LOAD_COMPLETE, loadedHandler ) ;

			}
			
			if( plane == null )
			{
				plane = new Plane( material, 800, 600, 2, 2 ) ;
				plane.name = "plane" ;
			}
			
			addChild( view ) ;
			view.scene.addChild( plane ) ;
		}
		
		private function rateHandler( event : Event ) : void
		{
			if( plane )
				plane.localRotationY-- ;
			if( view )
				view.singleRender() ;
		}
		
		private function loadedHandler( event : FileLoadEvent ) : void
		{
			material.tiled = true ; //打开平铺模式
			material.maxU = 8 ; //横向平铺数量，|所有材质将被拉伸以适合物体的尺寸
			material.maxV = 4 ; //纵向平铺数量。|因此要根据理想物体尺寸，创建合适大小的图片
		}
	}
}